package cn.com.xjnt.vas.ordering.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.frameworks.core.repository.BaseRepository;
import com.frameworks.core.service.AbstractBaseService;

import cn.com.xjnt.vas.ordering.domain.entity.User;
import cn.com.xjnt.vas.ordering.repository.UserRepository;
import cn.com.xjnt.vas.ordering.service.UserService;

/** 
* 类说明：
* @author 作者 LUXIN
* @version 创建时间：2016年5月6日18:48:26
*/
@Service
@Transactional
public class UserServiceImpl extends AbstractBaseService<User, Long> implements UserService {
    @Autowired
    private UserRepository userRepository;
    
    @Override
    protected BaseRepository<User, Long> getBaseRepository() {
        return userRepository;
    }

    @Override
    public void save(User entity) {
        super.saveOrUpdate(entity);
    }

    @Override
    public void update(User entity) {
        super.saveOrUpdate(entity);
    }

    @Override
    public boolean checkMobile(User user, String mobile) {
        boolean b = false;
        if(user.getId()==null){//新增
            List<User> lists = userRepository.findByMobile(mobile);
            if(lists.size() == 0){
                b = true;
            }
        }else{//修改
            User model = getBaseRepository().findOne(user.getId());
            if(mobile.equals(model.getMobile())){//未改变手机号
                b = true;
            }else{
                List<User> lists = userRepository.findByMobile(mobile);
                if(lists.size() == 0){
                    b = true;
                }
            }
        }
        return b;
    }

    @Override
    public List<User> findByMobile(String mobile) {
        List<User> lists = userRepository.findByMobile(mobile);
        return lists;
    }
}
 